package cn.lbf.stuniversity.equipmentbasic.service;

import cn.lbf.stuniversity.equipmentbasic.dao.EquipmentBasicDao;
import cn.lbf.stuniversity.equipmentbasic.entity.EquipmentBasic;
import cn.lbf.stuniversity.utils.PageUtil;
import cn.lbf.stuniversity.utils.ResultUtil;
import cn.lbf.stuniversity.vo.PageVO;
import cn.lbf.stuniversity.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lbf.stuniversity.constant.Code;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import cn.lbf.stuniversity.equipmentbasic.form.*;
@Service
public class EquipmentBasicServiceImpl implements EquipmentBasicService {
    @Autowired
    private EquipmentBasicDao equipmentBasicDao;

    @Override
    public List<EquipmentBasic> selectAll() {
        return equipmentBasicDao.selectAll();
    }

    @Override
    public Result<Map<String, Object>> addNew(EquipmentBasicAddForm form) {
        Map<String, Object> map = new HashMap<>();
        map.put("equipmentBasicId", form.getEquipmentBasicId());
        map.put("equipmentName", form.getEquipmentName());
        int equipmentCount = equipmentBasicDao.selectEquipBasicCountByIdAndName(map);
        if (equipmentCount > 0) {
            return new ResultUtil<Map<String, Object>>().setErrorMsg(Code.HAS_EXISTS, "已有同名的设备!");
        }
        int i = equipmentBasicDao.addNew(form);
        if (i > 0) {
            return new ResultUtil<Map<String, Object>>().setSuccessMsg("添加成功");
        } else {
            return new ResultUtil<Map<String, Object>>().setErrorMsg(Code.FAIL, "添加失败，请联系管理员!");
        }
    }

    @Override
    public Result<Page<EquipmentBasic>> queryList(EquipmentBasicForm form) {
        try {
            form.initOffSet();
            List<EquipmentBasic> list = equipmentBasicDao.selectEquipmentByIdAndName(form);
            long count = equipmentBasicDao.queryCount(form);
            PageVO pageVO = new PageVO(form.getPageNumber(), form.getPageSize());
            PageImpl<EquipmentBasic> page = new PageImpl<>(list, PageUtil.initPage(pageVO), count);
            return new ResultUtil<Page<EquipmentBasic>>().setData(page);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<Page<EquipmentBasic>>().setErrorMsg("查询有误 请联系管理员");
        }
    }

    @Override
    public Result<String> del(String id) {
        EquipmentBasic equipmentBasic = equipmentBasicDao.selectById(id);
        if (equipmentBasic == null) {
            return new ResultUtil<String>().setErrorMsg(Code.NOT_EXISTS, "设备不存在");
        }
        int remove = equipmentBasicDao.deleteByUpdateId(id);
        if (remove > 0) {
            return new ResultUtil<String>().setSuccessMsg("删除成功");
        } else {
            return new ResultUtil<String>().setErrorMsg(Code.FAIL, "删除失败 请联系管理员");
        }
    }

    @Override
    public Result<String> updateByEquipmentBasicId(EquipmentBasicDetailForm form) {
        EquipmentBasic equipmentBasic = equipmentBasicDao.selectById(form.getEquipmentBasicId());
        if (equipmentBasic == null) {
            return new ResultUtil<String>().setErrorMsg(Code.FAIL, "更新失败 查无此设备");
        }
        int updateNum = equipmentBasicDao.updateByEquipmentBasicId(form);
        if (updateNum > 0) {
            return new ResultUtil<String>().setSuccessMsg("更新成功!");
        }
        return new ResultUtil<String>().setErrorMsg("更新失败!");
    }
}
